package hk.org.ha.cdars.job;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import hk.org.ha.cdars.datacontext.BisDataContext.G1RefComboListCheckResult;
import hk.org.ha.cdars.datacontext.BisDataContext.G2RefComboListCheckResult;
import hk.org.ha.cdars.datacontext.BisDataContext.IpStep2ComboList;
import hk.org.ha.cdars.job.CheckJob.Flag;
import lava.rt.common.UtilCommon;


@Service
public class G2RefComboListCheckJob extends CheckJob<G2RefComboListCheckResult>{

	@Override
	public List<G2RefComboListCheckResult> check() throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		
				List<G2RefComboListCheckResult> rets=new ArrayList<>();
				
				List<IpStep2ComboList> comboLists=dataContext.IP_STEP2_COMBO_LIST.selectList("",CR.nodeType.asc);
				
				Map<String, IpStep2ComboList> columnComboMap=UtilCommon.toMap(comboLists, (col)->{
					 return col.getPath();
				});
				List<String> diffMsgs=new ArrayList<>();
				dataContext.G2_CRITERIAL_NODE.selectCursoring((index,g2node)->{
					diffMsgs.clear();
					G2RefComboListCheckResult ret=new G2RefComboListCheckResult();
					IpStep2ComboList combo=columnComboMap.get(g2node.getPath());
					if(combo==null) {
						   ret.setFlag(Flag.不存在.code);	
						}else if(compare(g2node, combo, diffMsgs)>0) {
							ret.setFlag(Flag.差異.code);
							ret.setRemark(String.join(";", diffMsgs));
							ret.setDescription(combo.getDescription());
						}
						else {
							ret.setFlag(Flag.相同.code);	
							ret.setDescription(combo.getDescription());
						}
					ret.setG2TreeType(g2node.getTreeType());
					ret.setG2NodeType(getNodeType(g2node));
					ret.setG2ColumnId(g2node.getColumnId());
					ret.setG2Path(g2node.getPath());
					ret.setJobNo(getJobNo());
					
					rets.add(ret);
					
					return 0;
				}, CR.remark.notEq("warning"), CR.criteriaType.asc);
				
				return rets;
	}

}
